package com.checking.attendance.controller;

import com.checking.attendance.pojo.Clock;
import com.checking.attendance.pojo.ClockRules;
import com.checking.attendance.pojo.Leave;
import com.checking.attendance.service.ClockService;
import com.checking.attendance.service.CourseService;
import com.checking.attendance.util.DateUtil;
import com.checking.attendance.util.ReturnUtil;
import com.checking.attendance.util.UuidUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping
@RestController
public class ClockJsonController {
    @Autowired
    private ClockService clockService;

    //添加一条考勤信息
    @ResponseBody
    @RequestMapping(value = "/member/clockInsert",method = RequestMethod.POST)
    public ModelMap clockInsert(@Validated @RequestBody Clock clock, BindingResult result){
        try {
            System.out.println(clock.toString());
            if(StringUtils.isEmpty(clock.getUserId())){
                return ReturnUtil.error("用户ID不能为空", null, null);
            }

            if (clock.getUserName() != null){
                clock.setUserName(clock.getUserName());
            }

            if (clock.getStartAttendTime() != null){
                clock.setStartAttendTime(clock.getStartAttendTime());
            }

            if (clock.getStartAttendState() != null){
                clock.setStartAttendState(clock.getStartAttendState());
            }

            if (clock.getEndAttendTime() != null){
                clock.setEndAttendTime(clock.getEndAttendTime());
            }

            if (clock.getEndAttendState() != null){
                clock.setEndAttendState(clock.getEndAttendState());
            }

            String id = UuidUtil.getUUID();
            clock.setId(id);
            clock.setCreateTime(DateUtil.getPresentDate());

            int count = clockService.insertSelective(clock);
            if (count == 0){
                return ReturnUtil.error("添加考勤信息失败");
            }

            return ReturnUtil.success("添加考勤信息成功");

        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }


    //根据用户id和当前时间查询一条考勤信息
    @ResponseBody
    @RequestMapping(value = "/member/clockSelectDayAttendance",method = RequestMethod.POST)
    public ModelMap clockSelectDayAttendance(@Validated @RequestBody Clock clock, BindingResult result){
        try {
            if(StringUtils.isEmpty(clock.getUserId())){
                return ReturnUtil.error("用户ID不能为空", null, null);
            }

            if(StringUtils.isEmpty(clock.getStartAttendDate())){
                return ReturnUtil.error("查询时间不能为空", null, null);
            }

            Clock clock1 = clockService.clockSelectDayAttendance(clock);

            if (clock1 == null){
                return ReturnUtil.error("暂无考勤信息");
            }

            return ReturnUtil.success("获取用户当天考勤信息成功", clock1);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }

    //根据考勤ID查询一条记录
    @ResponseBody
    @RequestMapping(value = "/member/queryDayAttendanceRecord",method = RequestMethod.POST)
    public ModelMap queryDayAttendanceRecord(@Validated @RequestBody Clock clock, BindingResult result){
        try {
            if(StringUtils.isEmpty(clock.getId())){
                return ReturnUtil.error("考勤ID不能为空", null, null);
            }


            Clock clock1 = clockService.selectByPrimaryKey(clock.getId());
            if (clock1 == null){
                return ReturnUtil.error("暂无考勤信息");
            }

            return ReturnUtil.success("获取用户当天考勤信息成功", clock1);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }

    //下课打卡签到
    @ResponseBody
    @RequestMapping(value = "/member/clockClassUpdate",method = RequestMethod.POST)
    public ModelMap clockClassUpdate(@Validated @RequestBody Clock clock, BindingResult result){
        try {
            if(StringUtils.isEmpty(clock.getId())){
                return ReturnUtil.error("考勤编号ID不能为空", null, null);
            }


            int count = clockService.updateByPrimaryKeySelective(clock);
            if (count == 0){
                return ReturnUtil.error("下课签到失败");
            }

            return ReturnUtil.success("下课签到成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }

    //根据当前用户id查询所有打卡记录
    @ResponseBody
    @RequestMapping(value = "/member/queryClockRecord",method = RequestMethod.POST)
    public ModelMap queryClockRecord(@Validated @RequestBody Clock clock, BindingResult result){
        try {
            if(StringUtils.isEmpty(clock.getUserId())){
                return ReturnUtil.error("用户ID不能为空", null, null);
            }

            if(StringUtils.isEmpty(clock.getCourseId())){
                return ReturnUtil.error("课程ID不能为空", null, null);
            }

            List<Clock> clocks = clockService.queryClockRecord(clock);
            if (clocks.size() == 0){
                return ReturnUtil.error("暂无考勤信息");
            }

            return ReturnUtil.success("获取用户打卡信息成功", clocks);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }

    //根据课程号查询一条记录信息
    @ResponseBody
    @RequestMapping(value = "/member/teacherQueryClockRecord",method = RequestMethod.POST)
    public ModelMap teacherQueryClockRecord(@Validated @RequestBody Clock clock, BindingResult result){
        try {
            if(StringUtils.isEmpty(clock.getCourseId())){
                return ReturnUtil.error("课程ID不能为空", null, null);
            }

            List<Clock> clocks = clockService.teacherQueryClockRecord(clock.getCourseId());
            if (clocks.size() == 0){
                return ReturnUtil.error("暂无考勤信息");
            }

            return ReturnUtil.success("获取用户打卡信息成功", clocks);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }

    //查询所有打卡记录
    @ResponseBody
    @RequestMapping(value = "/member/queryClockRecordAll",method = RequestMethod.POST)
    public ModelMap queryClockRecordAll(@Validated @RequestBody Clock clock, BindingResult result){
        try {

            List<Clock> clocks = clockService.queryClockRecordAll();
            if (clocks.size() == 0){
                return ReturnUtil.error("暂无考勤信息");
            }

            return ReturnUtil.success("获取用户打卡信息成功", clocks);
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }


    //删除一条考勤记录
    @ResponseBody
    @RequestMapping(value = "/member/deleteClock",method = RequestMethod.POST)
    public ModelMap deleteClock(@Validated @RequestBody Clock clock, BindingResult result){
        try {
            if(StringUtils.isEmpty(clock.getId())){
                return ReturnUtil.error("考勤ID不能为空", null, null);
            }

            int count = clockService.deleteByPrimaryKey(clock.getId());
            if (count == 0){
                return ReturnUtil.error("删除考勤信息失败");
            }

            return ReturnUtil.success("删除考勤信息成功");
        }catch (Exception e) {
            e.printStackTrace();
            return ReturnUtil.error("操作失败", null, null);
        }
    }
}
